package com.sqx.modules.app.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.dao.UserMoneyDao;
import com.sqx.modules.app.entity.UserMoney;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@AllArgsConstructor
@RequestMapping("/app/userMoney")
@Api("app 钱包 ")
public class AppUserMoneyController {

    private UserMoneyService userMoneyService;

    private UserMoneyDao userMoneyDao;

    /*
    * 获取用户的保证金额
    * */
    @GetMapping("/getSafetyMoney")
    @Login
    @ApiOperation("保证金额")
    public Result getSafetyMoneyData(@RequestAttribute Long userId) {
        BigDecimal safetyMoney = userMoneyDao.selectOne(new LambdaQueryWrapper<UserMoney>().eq(UserMoney::getUserId, userId)).getSafetyMoney();
        return Result.success().put("data", safetyMoney);
    }

    @GetMapping("/selectMyMoney")
    @Login
    @ApiOperation("我的钱包余额")
    public Result selectMyMoney(@RequestAttribute Long userId) {
        return Result.success().put("data", userMoneyService.selectUserMoneyByUserId(userId));
    }

    /**
     * 我的收益
     */
    @GetMapping("/selectMyProfit")
    @Login
    @ApiOperation("我的收益")
    public Result selectMyProfit(@RequestAttribute Long userId) {
        return Result.success().put("data", userMoneyService.selectMyProfit(userId));
    }

    /**
     * 付款接单
     */
    @GetMapping("/payTakingOrder")
    @Login
    @ApiOperation("付款接单或会员")
    public Result payTakingOrder(@RequestAttribute Long userId, Long orderId) {
        return Result.success().put("data", userMoneyService.payTakingOrder(userId, orderId));
    }



    /**
     * 余额明细
     */
    @GetMapping("/balanceDetailed")
    @Login
    @ApiOperation("余额明细")
    public Result balanceDetailed(@RequestAttribute Long userId, Long page, Long limit) {
        Page<UserMoneyDetails> pages = new Page<>(page, limit);
        return Result.success().put("data", userMoneyService.balanceDetailed(userId, pages));
    }


    /**
     * 收益明细
     */
    @GetMapping("/profitDetailed")
    @Login
    @ApiOperation("收益明细")
    public Result profitDetailed(@RequestAttribute Long userId, Long page, Long limit) {
        if (page != null || limit != null) {
            return Result.error("分页条件为空");
        } else {
            IPage ipage = new Page(page, limit);
            return Result.success().put("data", userMoneyService.profitDetailed(userId, ipage));
        }
    }

    @Login
    @PostMapping("/refundSafetMoney")
    @ApiOperation("退还保证金")
    public Result refundSafetMoney(@RequestAttribute Long userId){
        return userMoneyService.refundSafetMoney(userId);
    }

    @Login
    @ApiOperation("获取保证金明细")
    @GetMapping("/getBondDetails")
    public Result getBondDetails(@RequestAttribute Long userId,Integer page, Integer limit,UserMoneyDetails userMoneyDetails) {

        return Result.success().put("data", userMoneyService.getBondDetails(userId,page, limit, userMoneyDetails));
    }
}
